package
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;

	public class MapImage
	{
		public var bitmapData:BitmapData;
		public var id:uint;
		protected var path:String;
		protected var loaded:Boolean = false;
		protected var completeCallback:Function;
		public function MapImage(id:uint,path:String)
		{
			this.id = id;
			this.path = path;
		}
		public function Load(complete:Function = null):void
		{
			if(loaded == true)
			{
				return;
			}
			else
			{
				loaded = true;
			}
			completeCallback = complete;
			var loader:Loader = new Loader();
			var request:URLRequest = new URLRequest(path);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandlerObjects);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.load(request);
		}
		private function completeHandlerObjects(event:Event):void
		{
			bitmapData = event.currentTarget.content.bitmapData;
			if(completeCallback != null)
			{
				completeCallback(this);
			}
		}
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			trace("ioErrorHandler: " + event);
		}
		public function Draw(g:Graphics, x:int, y:int):void
		{
			if(bitmapData)
			{
				var matrix:Matrix = new Matrix();
				matrix.translate(x, y);
				g.beginBitmapFill(bitmapData,matrix);
				g.drawRect(x,y,bitmapData.width,bitmapData.height);
				g.endFill();
			}
		}
		public function GetWidth():uint
		{
			if(bitmapData == null)
			{
				return 0;
			}
			return bitmapData.width;
		}
		public function GetHeight():uint
		{
			if(bitmapData == null)
			{
				return 0;
			}
			return bitmapData.height;
		}
	}
}